﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL.Tbl;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// CPFollowInformation 的摘要说明
    /// </summary>
    public class CPFollowInformation : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string str = context.Request.QueryString["datasource"].ToString();
            int customeruid = 0;
            bool ret = false;
            if (int.TryParse(context.Request.QueryString["uid"], out customeruid) && !string.IsNullOrEmpty(str))
            {
                tblAddress address = new tblAddress();
                tblAddressBLL addressBLL=new tblAddressBLL();
                string[] temp = str.Split('_');
                if (temp[1] == "cp")
                {
                    tblContactPerson personENT = new tblContactPerson();
                    personENT.ContactPersonUID = Convert.ToInt32(temp[0]);
                    tblContactPersonBLL personBLL = new tblContactPersonBLL();
                    var cpENT = personBLL.GetListByENT(personENT);
                    if (cpENT.Count > 0)
                        address = cpENT.FirstOrDefault().tblAddress;
                }
                if (temp[1] == "cust") {
                    tblCustomer customerENT = new tblCustomer();
                    customerENT.CustomerUID = Convert.ToInt32(temp[0]);
                    tblCustomerBLL customerBLL = new tblCustomerBLL();
                    customerENT = customerBLL.GetEntity(customerENT);
                    if (customerENT != null)
                    {
                        if (customerENT.AddressUID > 0)
                        {
                            address.AddressUID = customerENT.AddressUID;
                            address = addressBLL.GetEntity(address);
                        }
                    }
                }
                else
                {
                    tblCust_Branch branchENT = new tblCust_Branch();
                    branchENT.CustBranchUID = Convert.ToInt32(temp[0]);
                    tblCust_BranchBLL branchBLL = new tblCust_BranchBLL();
                    var branchList = branchBLL.GetListByENT(branchENT);
                    if (branchList.Count > 0)
                    {
                        int addressuid = Convert.ToInt32(branchList.FirstOrDefault().AddressUID);
                        if (addressuid > 0)
                        {
                            address.AddressUID = addressuid;
                            address = addressBLL.GetEntity(address);
                        }
                    }
                }
                StringBuilder strBuilder = new StringBuilder();
                string strReturn = string.Empty;
                strBuilder.Append("{");
                if (address != null)
                {
                    strBuilder.Append("\"Area_Chi\":\"" + address.Area_Chi + "\",");
                    strBuilder.Append("\"Area_Eng\":\"" + address.Area_Eng + "\",");
                    strBuilder.Append("\"City_Chi\":\"" + address.City_Chi + "\",");
                    strBuilder.Append("\"City_Eng\":\"" + address.City_Eng + "\",");
                    strBuilder.Append("\"CountryUID\":\"" + address.CountryUID + "\",");
                    strBuilder.Append("\"District_Chi\":\"" + address.District_Chi + "\",");
                    strBuilder.Append("\"District_Eng\":\"" + address.District_Eng + "\",");
                    strBuilder.Append("\"Flat_Bldg_Chi\":\"" + address.Flat_Bldg_Chi + "\",");
                    strBuilder.Append("\"Flat_Bldg_Eng\":\"" + address.Flat_Bldg_Eng + "\",");
                    strBuilder.Append("\"FullAdd_Chi\":\"" + address.FullAdd_Chi + "\",");
                    strBuilder.Append("\"FullAdd_Eng\":\"" + address.FullAdd_Eng + "\",");
                    strBuilder.Append("\"PostalCode\":\"" + address.PostalCode + "\",");
                    strBuilder.Append("\"Province_Chi\":\"" + address.Province_Chi + "\",");
                    strBuilder.Append("\"Province_Eng\":\"" + address.Province_Eng + "\",");
                    strBuilder.Append("\"Street_Rd_Chi\":\"" + address.Street_Rd_Chi + "\",");
                    strBuilder.Append("\"Street_Rd_Eng\":\"" + address.Street_Rd_Eng + "\",");
                    strBuilder.Append("\"AreaUID\":\"");
                    if (address.mtblCountry != null)
                    {
                        if (address.mtblCountry.AreaUID != null)
                        {
                            strBuilder.Append(address.mtblCountry.AreaUID);
                        }
                        else
                        { strBuilder.Append(0); }
                    }
                    strBuilder.Append("\",");
                }
                strBuilder.Append("}");
                strReturn = strBuilder.ToString();
                context.Response.Write(strReturn);
                context.Response.Flush();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}